/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import hibernate.model.dao.BinhLuanDAO;
import hibernate.model.dao.SanPhamDAO;
import hibernate.pojo.Binhluan;
import hibernate.pojo.Khachhang;
import hibernate.pojo.Sanpham;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author phathuynh
 */
@WebServlet(name = "commentcontroller", urlPatterns = {"/commentcontroller"})
public class commentcontroller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
            HttpSession session = request.getSession();
            String action = request.getParameter("action");
            Khachhang khachHang = (Khachhang) session.getAttribute("KhachHang");
            if(khachHang == null)
            {
                request.getRequestDispatcher("logincontroller?action=login").forward(request, response);
            }
            if(action.equals("newcomment"))
            {
                // bat parem maSanPham roi send
                String productID = request.getParameter("maSanPham");
                request.setAttribute("maSanPhamBinhLuan", productID);
                int maSanPham = -1;
                if(productID!=null)
                {
                    maSanPham = Integer.parseInt(productID);
                }
                List<Binhluan> lstBinhLuan = BinhLuanDAO.DanhSachBinhLuanUngVoiSanPham(maSanPham);
                request.setAttribute("DanhSachBinhLuanUngVoiSanPham", lstBinhLuan);
                request.getRequestDispatcher("pagestile/dealdetailcomment.jsp").forward(request, response);
            }
            else if(action.equals("sendnewcomment"))
            {
                // getParamater(textarea);
                // sau do luu noi dung xuong csdl, bang binhluan.
                // Neu luu thanh cong thi chuyen ve trang xem chi tiet san pham & load lai noi dung comment cua minh.
                String strComment = request.getParameter("textarea");
                String productID = request.getParameter("maSanPham");
                Sanpham product = SanPhamDAO.getProductByID(Integer.parseInt(productID));
                Khachhang customer = (Khachhang) session.getAttribute("KhachHang");
                
                Date dateComment = new Date();
                Binhluan comment = new Binhluan();
                comment.setSanpham(product);
                comment.setKhachhang(khachHang);
                comment.setNgayBinhLuan(dateComment);
                comment.setNoiDungBinhLuan(strComment);
                boolean commentRes = BinhLuanDAO.addComment(comment);
                if(commentRes==true)
                {
                    request.setAttribute("maSanPhamBinhLuan", productID);
                    request.getRequestDispatcher("pagestile/dealdetailcomment.jsp").forward(request, response);
                }
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
